package com.ld.security.common.core.util;

import lombok.SneakyThrows;

import java.io.ByteArrayOutputStream;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class StringUtil {

    static Pattern p = Pattern.compile("\\s*|\t|\r|\n");
    private static final int charMargin = 32;
    /**
     * 删除换行符
     * @param str
     * @return
     */
    public static String removeLine(String str){
        Matcher m = p.matcher(str);
        String content = m.replaceAll("");
        return content;
    }

    public static boolean isNull(String s){
        if(s == null || s.length() == 0) {
            return true;
        }
        return false;
    }
    public static boolean isNotNull(String s){

        return !isNull(s);
    }

  public static boolean isLowerCaseFirstOne(char c) {
      if(c >= 'A' && c <= 'Z'){
          return true;
      }
      return false;
  }

        //首字母转小写
    public static char toLowerCaseFirstOne(char c){
        if(c >= 'A' && c <= 'Z'){
            c += charMargin;
        }
        return c;
    }
    //首字母转大写
    public static char toUpperCaseFirstOne(char c){
        if(c >= 'a' && c <= 'z'){
            c -= charMargin;
        }
        return c;
    }
    //首字母转小写
    public static String toLowerCaseFirstOne(String s){
        char[] cs = s.toCharArray();
        cs[0] = toLowerCaseFirstOne(cs[0]);

        return new String(cs);
    }


    //首字母转大写
    public static String toUpperCaseFirstOne(String s){
        char[] cs = s.toCharArray();
        cs[0] = toUpperCaseFirstOne(cs[0]);

        return new String(cs);
    }



    @SneakyThrows
    public static byte[] createTemplateZip(Map<String,String> map ){

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream out = new ZipOutputStream(outputStream);

        map.forEach((name,val) -> {
            try{
                out.putNextEntry(new ZipEntry(name));
                out.write(val.getBytes());

            }catch (Exception e){
                e.printStackTrace();
            }
        });


        out.close();

        return outputStream.toByteArray();
    }


    public static String javaToColumn(String columnName){
        char[] cs = columnName.toCharArray();
        StringBuffer sb = new StringBuffer();
        for(char c:cs){
            if(c >='A' && c<='Z'){
                sb.append("_"+toLowerCaseFirstOne(c));
            }else{
                sb.append(c);
            }

        }
        return sb.toString();
    }

    public static String columnToJava(String columnName){
        char[] cs = columnName.toCharArray();
        StringBuffer sb = new StringBuffer();
        boolean b = false;
        for(char c:cs){
            if(c == '_'){
                b = true;
                continue;
            }
            if(b){
                c = toUpperCaseFirstOne(c);
                b=false;
            }
            sb.append(c);

        }
        return sb.toString();
    }


    public static String tableToClass(String tableName){
        tableName = columnToJava(tableName);
        return toUpperCaseFirstOne(tableName);
    }


}
